<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Class</title>
</head>
<body>
<a href="http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html">
    Javascript定义类（class）的三种方法
</a>
<script>
    var Animal = {
        createNew: function()
        {
            var animal = {};
            animal.sleep = function(){ alert("睡懒觉"); };
            return animal;
        }
    };

    var Cat = {

        sound : "喵喵喵2",

        createNew: function()
        {
            // 定义一个实例对象，把这个实例对象作为返回值。
//            var cat = {};
            //继承  cat实例，就会同时继承Cat类和Animal类。
            var cat = Animal.createNew();

            var sound = "喵喵喵1";

            cat.name = "大毛";

            cat.perSound = function(){alert(sound)};

            cat.makeSound = function(){ alert(Cat.sound); };

            cat.changeSound = function(x){ Cat.sound = x; };

            return cat;
        }
    };
    var cat1 = Cat.createNew();
    var cat2 = Cat.createNew();
    cat1.makeSound(); // 喵喵喵

    cat1.sleep(); // 睡懒觉

    cat1.perSound();

    alert(cat1.sound); // undefined

    cat2.changeSound("啦啦啦");
    cat1.makeSound(); // 啦啦啦
</script>
</body>
</html>